package StudentService;
import Student.Student;
import StudentDao.StudentDao;

public class StudentService {
    public boolean addStudent(Student stu) {
        //创建库管类型StudentDao
        StudentDao studentDao = new StudentDao();
        //将学生对象传给库管中的addStudent方法返回并接收一个布尔值
        return studentDao.addStudent(stu);
    }
    private StudentDao studentDao = new StudentDao();
    public boolean isExists(String id) {
        Student[] stus = studentDao.fullStudent();
        //默认设定一个index并赋值为false
        boolean index = false;
        //使用for循环进行遍历查找写入的id是否存在
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if(student != null && id.equals(student.getId())){
                index = true;
                break;
            }
        }
        return index;
    }


    public Student[] findAllStudent() {
        //指派库管拿出数组
        Student[] stus = studentDao.fullStudent();
        //判断数组是否为空
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if(stu!=null){
                index = i;
                break;
            }
        }
        //根据以上结果判断返回值
        if(index==-1){
            return null;
        }
        else {
            return stus;
        }
    }

    public void deleteStudentById(String delId) {
        //通知库管删除delId学生
        studentDao.deleteStudentById(delId);
    }

    public void updateStudent(String updateId, Student newstu) {
        studentDao.updateStudent(updateId,newstu);
    }
}
